package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //主页,我们默认主页一定要登录之后才能访问
        //如果session为空, 返回null, 说明用户还未登录, 跳转到login.html
        HttpSession session = req.getSession(false);
        if (session ==null){
            resp.sendRedirect("login.html");
            return;
        }
        String name = (String)session.getAttribute("name");
        Integer loginCount = (Integer) session.getAttribute("loginCount");
        loginCount++;
        session.setAttribute("loginCount",loginCount);
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("登录用户:"+name+",登录次数:"+loginCount);



    }
}
